package com.seselin.joke.http;

import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import java.util.HashMap;

/**
 * Created by Seselin on 2016/7/21.
 */
public class HttpExecutor extends Thread {

    public static final int HTTP_TAG = 1001;
    public static final String KEY_ID = "f4f353724cc2afcf3c1c7bcafd86fdad";

    private int pager;
    private Handler handler;

    public HttpExecutor(Handler handler, int pager) {
        this.handler = handler;
        this.pager = pager;
    }


    @Override
    public void run() {
        Message mg = Message.obtain();
        mg.what = HTTP_TAG;

        HashMap<String, Object> param = new HashMap<>();
        String url = "http://japi.juhe.cn/joke/content/list.from";
        param.put("page", pager);
        param.put("pagesize", 10);
        param.put("time", 1418745237);
        param.put("sort", "asc");
        param.put("key", KEY_ID);

        try {
            String result = HttpUtil.request(url, param);
            Gson gson = new Gson();
            mg.obj = gson.fromJson(result, JokeBean.class);
        } catch (JsonSyntaxException e) {
            mg.obj = null;
            e.printStackTrace();
        }

        handler.sendMessage(mg);
    }
}
